package com.busi.wms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.busi.wms.model.WmsMaterial;
import com.jfinal.plugin.activerecord.Record;
import com.zcurd.common.tool.DBTool;
import com.zcurd.common.util.StringUtil;
import com.zcurd.controller.BaseController;
import com.zcurd.ext.render.csv.CsvRender;

/**
 * 物料信息表
 */
public class WmsMaterialController extends BaseController {
	
	public void listPage() {
		setAttr("dictDataunit_name", WmsMaterial.me.getDictDataunit_name());
		setAttr("dictDatacate_name", WmsMaterial.me.getDictDatacate_name());
		setAttr("dictDataIs_delete", WmsMaterial.me.getDictDataIs_delete());
		render("list.html");
	}
	
	public void listData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id asc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_material", properties, symbols, values, orderBy, getPager());
		Map<String, Object> dictDataIs_delete = WmsMaterial.me.getDictDataIs_delete();
		for(Record record : list) {
			String fieldName = "Is_delete";
			if(dictDataIs_delete.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataIs_delete.get(record.get(fieldName).toString()));
			}
		}
		
		renderDatagrid(
			list, 
			DBTool.countByMultProperties("wms_material", properties, symbols, values)
		);
	}
	//物料检索公共页面
	public void queryMatData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id asc";
		}		
		List<Record> list = DBTool.findByMultProperties("wms_material", properties, symbols, values, orderBy, getPager());
				
		renderDatagrid(
			list, 
			DBTool.countByMultProperties("wms_material", properties, symbols, values)
		);
	}
	//增加页面
	public void addPage() {
		setAttr("dictDataunit_name", WmsMaterial.me.getDictDataunit_name());
		setAttr("dictDatacate_name", WmsMaterial.me.getDictDatacate_name());
		setAttr("dictDataIs_delete", WmsMaterial.me.getDictDataIs_delete());
		render("add.html");
	}
	
	//增加
	public void add() {
		getModel(WmsMaterial.class, "model").save();
		
		addOpLog("[物料信息表] 增加");
		renderSuccess();
	}
	
	//修改页面
	public void updatePage() {
		setAttr("dictDataunit_name", WmsMaterial.me.getDictDataunit_name());
		setAttr("dictDatacate_name", WmsMaterial.me.getDictDatacate_name());
		setAttr("dictDataIs_delete", WmsMaterial.me.getDictDataIs_delete());
		setAttr("model", WmsMaterial.me.findById(getPara("id")));
		render("update.html");
	}
	
	//修改
	public void update() {
		WmsMaterial model = WmsMaterial.me.findById(getPara("id"));
		model.set("mat_name", getPara("model.mat_name"));
		model.set("cur_num", getPara("model.cur_num"));
		model.set("unit_name", getPara("model.unit_name"));
		model.set("cate_name", getPara("model.cate_name"));
		model.set("mat_std", getPara("model.mat_std"));
		model.set("Is_delete", getPara("model.Is_delete"));
		model.set("create_time", getPara("model.create_time"));
		model.update();
		addOpLog("[物料信息表] 修改");
		renderSuccess();
	}
	
	//删除
	public void delete() {
		Integer[] ids = getParaValuesToInt("id[]");
		for (Integer id : ids) {
			new WmsMaterial().set("id", id).delete();
			
		}
		
		addOpLog("[物料信息表] 删除");
		renderSuccess();
	}
	
	//详情页面
	public void detailPage() {
		WmsMaterial model = WmsMaterial.me.findById(getParaToInt("id"));
		/*Map<String, Object> dictDataunit_name = WmsMaterial.me.getDictDataunit_name();
		if(dictDataunit_name.get(model.get("unit_name").toString()) != null) {
			model.set("unit_name", dictDataunit_name.get(model.get("unit_name").toString()));
		}
		Map<String, Object> dictDatacate_name = WmsMaterial.me.getDictDatacate_name();
		if(dictDatacate_name.get(model.get("cate_name").toString()) != null) {
			model.set("cate_name", dictDatacate_name.get(model.get("cate_name").toString()));
		}*/
		Map<String, Object> dictDataIs_delete = WmsMaterial.me.getDictDataIs_delete();
		if(dictDataIs_delete.get(model.get("Is_delete").toString()) != null) {
			model.set("Is_delete", dictDataIs_delete.get(model.get("Is_delete").toString()));
		}
		setAttr("model", model);
		render("detail.html");
	}
	
	//导出csv
	public void exportCsv() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_material", properties, symbols, values);
		/*Map<String, Object> dictDataunit_name = WmsMaterial.me.getDictDataunit_name();
		for(Record record : list) {
			String fieldName = "unit_name";
			if(dictDataunit_name.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataunit_name.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDatacate_name = WmsMaterial.me.getDictDatacate_name();
		for(Record record : list) {
			String fieldName = "cate_name";
			if(dictDatacate_name.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatacate_name.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDataIs_delete = WmsMaterial.me.getDictDataIs_delete();
		for(Record record : list) {
			String fieldName = "Is_delete";
			if(dictDataIs_delete.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataIs_delete.get(record.get(fieldName).toString()));
			}
		}*/
		
		List<String> headers = new ArrayList<String>();
		List<String> clomuns = new ArrayList<String>();
		headers.add("物料名称");
		clomuns.add("mat_name");
		headers.add("当前库存");
		clomuns.add("cur_num");
		headers.add("单位");
		clomuns.add("unit_name");
		headers.add("分类名");
		clomuns.add("cate_name");
		headers.add("物料规格描述");
		clomuns.add("mat_std");
		headers.add("是否删除");
		clomuns.add("Is_delete");
		headers.add("创建时间");
		clomuns.add("create_time");
		
		CsvRender csvRender = new CsvRender(headers, list);
		csvRender.clomuns(clomuns);
		csvRender.fileName("物料信息表");
		
		addOpLog("[物料信息表] 导出cvs");
		render(csvRender);
	}
}
